/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legsem.legstar.cci;

import java.util.Map;
import java.util.Map.Entry;

import com.legstar.coxb.transform.IHostToJavaTransformer;
import com.legstar.coxb.transform.IHostTransformer;
import com.legstar.coxb.transform.IJavaToHostTransformer;

/**
 * Describes an interaction with a the host with multiple input structures and
 * multiple output structures.
 * <p/>
 * Following the JCA specification the properties are bound.
 */
public class LegStarMultiIOInteractionSpecImpl extends
        LegStarInteractionSpecImpl implements LegStarMultiIOInteractionSpec {

    private static final long serialVersionUID = -8575199298202957925L;

    private Map < String, IJavaToHostTransformer > inputTransformers;
    private Map < String, IHostToJavaTransformer > outputTransformers;

    public Map < String, IJavaToHostTransformer > getInputTransformers() {
        return inputTransformers;
    }

    public void setInputTransformers(
            Map < String, IJavaToHostTransformer > transformers) {
        Map < String, IJavaToHostTransformer > oldInputTransformers = inputTransformers;
        inputTransformers = transformers;
        firePropertyChange("InputTransformers", oldInputTransformers,
                inputTransformers);

    }

    public Map < String, IHostToJavaTransformer > getOutputTransformers() {
        return outputTransformers;
    }

    public void setOutputTransformers(
            Map < String, IHostToJavaTransformer > transformers) {
        Map < String, IHostToJavaTransformer > oldOutputTransformers = outputTransformers;
        outputTransformers = transformers;
        firePropertyChange("OutputTransformers", oldOutputTransformers,
                outputTransformers);

    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("LegStarMultiIOInteractionSpecImpl [inputTransformers=");
        builder.append(toString(inputTransformers));
        builder.append(", outputTransformers=");
        builder.append(toString(outputTransformers));
        builder.append(", ");
        builder.append(super.toString());
        builder.append("]");
        return builder.toString();
    }

    private String toString(Map < String, ? extends IHostTransformer > map) {
        if (map == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        boolean first = true;
        for (Entry < String, ? extends IHostTransformer > entry : map
                .entrySet()) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(entry.getKey());
            builder.append(":");
            builder.append(entry.getValue().getClass().getName());
        }
        builder.append("]");
        return builder.toString();
    }

}
